<?php
/**
* @file $Id: CopySchool.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$tables = array('ATTENDANCE_CALENDARS'=>_('Calendars'),'SCHOOL_PERIODS'=>_('School Periods'),'SCHOOL_YEARS'=>_('Marking Periods'),'REPORT_CARD_GRADES'=>_('Report Card Grade Codes'),'REPORT_CARD_COMMENTS'=>_('Report Card Comment Codes'),'ELIGIBILITY_ACTIVITIES'=>_('Eligibility Activity Codes'),'ATTENDANCE_CODES'=>_('Attendance Codes'),'SCHOOL_GRADELEVELS'=>_('Grade Levels'));

$table_list = '<TABLE align='.ALIGN_LEFT.'>';
foreach($tables as $table=>$name)
{
	$table_list .= '<TR><TD><INPUT type=checkbox value=Y name=tables['.$table.'] CHECKED></TD><TD>'.$name.'</TD></TR>';
}
$table_list .= '</TABLE></CENTER><BR><small>'._('New School\'s Title').'</small> <INPUT type=text name=title value="'._('New School').'"><CENTER>';

DrawHeader(ProgramTitle());

if(Prompt(_('Confirm Copy School'),_('Are you sure you want to copy the data for').' '.GetSchool(UserSchool()).' '._('to a new school').'?',$table_list))
{
	if(count($_REQUEST['tables']))
	{
		$id = DBGet(DBQuery("SELECT ".db_seq_nextval('SCHOOLS_SEQ')." AS ID".FROM_DUAL));
		$id = $id[1]['ID'];
		DBQuery("INSERT INTO SCHOOLS (ID,TITLE) values('".$id."','".str_replace("\'","''",$_REQUEST['title'])."')");
		foreach($_REQUEST['tables'] as $table=>$value)
			_rollover($table);
	}
	echo '<FORM action=Modules.php?modname='.$_REQUEST['modname'].' method=POST>';
	echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
	DrawHeader('<IMG SRC=assets/check.gif>'._('The data have been copied to a new school called').' "'.$_REQUEST['title'].'".','<INPUT type=submit value='._('OK').'>');
	echo '</FORM>';
	unset($_SESSION['_REQUEST_vars']['tables']);
	unset($_SESSION['_REQUEST_vars']['delete_ok']);
}

function _rollover($table)
{	global $id;

	switch($table)
	{
		case 'SCHOOL_PERIODS':
			DBQuery("INSERT INTO SCHOOL_PERIODS (PERIOD_ID,SYEAR,SCHOOL_ID,SORT_ORDER,TITLE,SHORT_NAME,LENGTH,ROLLOVER_ID) SELECT nextval('SCHOOL_PERIODS_SEQ'),SYEAR,'$id' AS SCHOOL_ID,SORT_ORDER,TITLE,SHORT_NAME,LENGTH,ROLLOVER_ID FROM SCHOOL_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'SCHOOL_GRADELEVELS':
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SCHOOL_ID' && $column!='NEXT_GRADE_ID')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SCHOOL_ID".$columns.") SELECT nextval('".$table."_SEQ'),'$id' AS SCHOOL_ID".$columns." FROM $table WHERE SCHOOL_ID='".UserSchool()."'");
		break;

		case 'SCHOOL_YEARS':
			DBQuery("INSERT INTO SCHOOL_YEARS (MARKING_PERIOD_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),SYEAR,'$id' AS SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,MARKING_PERIOD_ID FROM SCHOOL_YEARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("INSERT INTO SCHOOL_SEMESTERS (MARKING_PERIOD_ID,YEAR_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_YEARS y WHERE y.SYEAR=s.SYEAR AND y.ROLLOVER_ID=s.YEAR_ID AND y.SCHOOL_ID='$id') AS YEAR_ID,SYEAR,'$id' AS SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("INSERT INTO SCHOOL_QUARTERS (MARKING_PERIOD_ID,SEMESTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_SEMESTERS s WHERE s.SYEAR=q.SYEAR AND s.ROLLOVER_ID=q.SEMESTER_ID AND s.SCHOOL_ID='$id') AS SEMESTER_ID,SYEAR,'$id' AS SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,REGISTRATION_START_DATE,REGISTRATION_END_DATE,YEAR_FRACTION,MARKING_PERIOD_ID FROM SCHOOL_QUARTERS q WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("INSERT INTO SCHOOL_PROGRESS_PERIODS (MARKING_PERIOD_ID,QUARTER_ID,SYEAR,SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,ROLLOVER_ID) SELECT nextval('MARKING_PERIOD_SEQ'),(SELECT MARKING_PERIOD_ID FROM SCHOOL_QUARTERS q WHERE q.SYEAR=p.SYEAR AND q.ROLLOVER_ID=p.QUARTER_ID AND q.SCHOOL_ID='$id'),SYEAR,'$id' AS SCHOOL_ID,TITLE,SHORT_NAME,SORT_ORDER,START_DATE,END_DATE,POST_START_DATE,POST_END_DATE,MARKING_PERIOD_ID FROM SCHOOL_PROGRESS_PERIODS p WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'ATTENDANCE_CALENDARS';
			DBQuery("INSERT INTO ATTENDANCE_CALENDARS (SCHOOL_ID,TITLE,SYEAR,CALENDAR_ID,DEFAULT_CALENDAR,ROLLOVER_ID) SELECT '$id' AS SCHOOL_ID,TITLE,SYEAR,nextval('CALENDARS_SEQ'),DEFAULT_CALENDAR,CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");

			DBQuery("INSERT INTO ATTENDANCE_CALENDAR (SYEAR,SCHOOL_ID,SCHOOL_DATE,MINUTES,BLOCK,CALENDAR_ID) SELECT SYEAR,'$id' AS SCHOOL_ID,SCHOOL_DATE,MINUTES,BLOCK,(SELECT CALENDAR_ID FROM ATTENDANCE_CALENDARS y WHERE y.SYEAR=s.SYEAR AND y.ROLLOVER_ID=s.CALENDAR_ID AND y.SCHOOL_ID='$id') AS CALENDAR_ID FROM ATTENDANCE_CALENDAR s WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;

		case 'REPORT_CARD_GRADES':
		case 'REPORT_CARD_COMMENTS':
		case 'ELIGIBILITY_ACTIVITIES':
		case 'ATTENDANCE_CODES':
			$table_properties = db_properties($table);
			$columns = '';
			foreach($table_properties as $column=>$values)
			{
				if($column!='ID' && $column!='SYEAR' && $column!='SCHOOL_ID')
					$columns .= ','.$column;
			}
			DBQuery("INSERT INTO $table (ID,SYEAR,SCHOOL_ID".$columns.") SELECT nextval('".$table."_SEQ'),SYEAR,'$id' AS SCHOOL_ID".$columns." FROM $table WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'");
		break;
	}
}
?>